package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.GoodsDao;
import com.hs.panghu.dao.impl.GoodsDaoImpl;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.Result;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        // 连接数据库
        GoodsDao goodsDao = new GoodsDaoImpl();
        if ("goodsList".equals(action)) {  // 分页查询
            Map<String, String> map = new HashMap<>();
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String goodsName = req.getParameter("goodsName");
            String goodsStatus = req.getParameter("goodsStatus");
            System.out.println(goodsStatus);
            String shopName = req.getParameter("ShopName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("goodsName", goodsName);
            map.put("goodsStatus", goodsStatus);
            map.put("shopName", shopName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);

            // 动态获取社区集合信息
            List<Goods> list = goodsDao.selectGoods(map);
            // 动态获取社区的数据总条数
            int total = goodsDao.selectGoodsCount(map);
            // 创建一个返回值的map集合，用来包裹list数据和total总数量
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            //把结果集合，封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("goodsStatus".equals(action)) {
            String goodsId = req.getParameter("goodsId");
            String goodsStatus = req.getParameter("goodsStatus");
            //组成对象
            Goods goods = new Goods(
                    Integer.parseInt(goodsId),
                    Integer.parseInt(goodsStatus)
            );
            int row = goodsDao.updateStatus(goods);
            if (row != 0) {
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            } else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
